***********************************************************
* Source: KwaraIIPanel4Sep12_vs1.dta: data from 2009, subsample: acute illnesses
* Action: impute the prices paid to alternative not chosen
* Output: pricelong_v4.dta
***********************************************************
* set the cd on the Replication_data folder
**********************************************************
 clear
 set more off

use "KwaraIIPanel4Sep12_2009_acuteillnesses.dta"


*generate the type of illness 
gen typeill  = q02_25
tab q02_25, nola miss


replace typeill = 5 if q02_25 == 25

///*** diarrhea and vomting
replace typeill = 3 if q02_25s == "VOMITING" 

//***injury & traffic accident 
replace typeill = 4 if q02_25s == "BURNS" 
replace typeill = 4 if q02_25s == "DISLOCATION" 
replace typeill = 4 if q02_25s == "FELL FROM TREE" 
replace typeill = 4 if q02_25s == "FRACTURE" 
replace typeill = 4 if q02_25s == "HOME ACCIDENT" 
replace typeill = 4 if q02_25s == "INJURY" 
replace typeill = 4 if q02_25s == "KNEE INJURY" 
replace typeill = 4 if q02_25s == "KNEE INJURY" 
replace typeill = 4 if q02_25s == "LACERATED WOUND" 
replace typeill = 4 if q02_25s == "STUNG BY BEE" 
replace typeill = 4 if q02_25s == "SWELLING LEG" 
replace typeill = 4 if q02_25s == "SWELLING OF FINGER" 
replace typeill = 4 if q02_25s == "WOUND" 

* Other : pain +generic syntoms

replace typeill = 3 if q02_25s == "ABDOMINAL PAIN" 
replace typeill = 3 if q02_25s == "BODY PAIN" 
replace typeill = 3 if q02_25s == "CHEST PAIN" 
replace typeill = 3 if q02_25s == "HEADACHE" 
replace typeill = 3 if q02_25s == "STOMACH PAIN" 
replace typeill = 3 if q02_25s == "WAIST PAIN" 
replace typeill = 3 if q02_25s == "MIGRINE" 
//***general sintoms
replace typeill = 3 if q02_25s == "DIZZINESS" 
replace typeill = 3 if q02_25s == "GENERAL WEAKNESS" 
replace typeill = 3 if q02_25s == "HYPERTENSION" 
replace typeill = 3 if q02_25s == "STRESS" 
replace typeill = 3 if q02_25s == "UNCONSCIOUS" 

//**body specific problems and infection
replace typeill = 3 if q02_25s == "CATTARH" 
replace typeill = 3 if q02_25s == "EYE PROBLEM" 
replace typeill = 3 if q02_25s == "EAR PROBLEM" 
replace typeill = 3 if q02_25s == "HERNIA" 
replace typeill = 3 if q02_25s == "MOUTH TRUSH" 
replace typeill = 3 if q02_25s == "NECK PROBLEM" 
replace typeill = 3 if q02_25s == "PALM" 
replace typeill = 3 if q02_25s  == "SEPTIC FINGER" 
replace typeill = 3 if q02_25s  == "SKIN INFECTION" 
replace typeill = 3 if q02_25s  == "ULCER" 
replace typeill = 3 if q02_25s  == "URINARY INFECTION" 
replace typeill = 3 if q02_25s  == "WITHLOW" 
replace typeill = 3 if q02_25s  == "BPH"
replace typeill = 3 if q02_25s  == "ABORTION"
replace typeill = 3 if q02_25s  == "PREGNANCY"
replace typeill = 3 if q02_25s  == "MULTIPLE BOILS" 
replace typeill = 3 if q02_25s  == "MUMPS" 
replace typeill = 3 if q02_25s  == "ANAEMIA"
replace typeill = 3 if q02_25s  == "Diabetes" 
replace typeill = 3 if q02_25s  == "FOR ANC"
replace typeill = 3 if q02_25s  == "BOIL"

* what is in other (q02_25==25)?
list q02_25 q02_25s if q02_25==25
count if q02_25==. 

* Unknown (missing)
replace typeill = 5 if typeill == . 

label define ill3 1 "FEBRILE ILLNESS" 2 "RESPIRATORY DISEASE" 3 "INFECTIONS AND PAIN" ///
4 "TRAFFIC ACCIDENT/INJURY" 5  "OTHER AND UNKNOWN"
label values typeill ill3

tab typeill 

tab q02_30, miss

* recover the facility in the study region from "other"
replace q02_30=14 if q02_30s=="AWOYE ORO" & q02_30==25
replace q02_30=39 if q02_30s=="GENERAL HOSPITAL IJAGBO" & q02_30==25
replace q02_30=32 if q02_30s=="GENERAL HOSPITAL OFFA" & q02_30==25

replace q02_30=70 if q02_30s=="PHC BUARI" & q02_30==25
replace q02_30=70 if q02_30s=="HEALTH CENTER BUARI" & q02_30==25

replace q02_30=79 if q02_30s=="YINKA CLINIC" & q02_30==25

replace q02_30=40 if q02_30s=="PHC CENTER IJAGBO" & q02_30==25
replace q02_30=40 if q02_30s=="PHC IJAGBO" & q02_30==25
replace q02_30=40 if q02_30s=="IJAGBO PHC" & q02_30==25

replace q02_30=39 if q02_30s=="HEALTH CENTRE IJAGBO" & q02_30==25

replace q02_30=63 if q02_30s=="ORE OFE CLINIC" & q02_30==25
replace q02_30=58 if q02_30s=="OLAYINKA HOSPITAL" & q02_30==25

replace q02_30=67 if q02_30s=="IGBO ARAN HEALTH CENTRE" & q02_30==25



tab q02_30 typeill

*price per facility 
table q02_30 typeill, c(mean q02_40g sd q02_40g)

** drop the Other facility not specified

*drop if q02_30==25 // drop facilities "other" not included in the quality assessment
tab q02_29a, nola

* generate indicator doctor and self-care "facility" numbers
gen  self = 1 if q02_29a == 0 | q02_29a == 9 | q02_29a == 10 | q02_29a == 12 |  q02_29a==13 | q02_29a ==14
gen  doctor = 1 if q02_29a == 6 | q02_29a == 7 | q02_29a == 8 | q02_29a == 11

tab q02_29a if self==1, nola

* Category SELF includes: NOBODY, TRADITIONAL HEALER AND SPIRITUAL HEALER, PRIVATE PARAMEDIC, PATENT MEDICINE VENDOR, MEDICINE PROVIDER< RELIGIOUS PERSON.
* Category DOCTOR includes: PRIVATE DOCTOR, PRIVATE NURSE, PRIVATE MIDWIFE, PHARMACIST

* attribute facility number 99= self 999==doctor
replace q02_30=99 if self==1 & q02_30==. 
replace q02_30=999 if doctor==1 & q02_30==.


* foreach facility generate a price variable: 
*attribute the mean of observed price by type of illness
*if no price is observed impute the mean price
 
 * total cost should be positive
 replace q02_40g=. if q02_40g==0
 
 *==========================================
* 1. Imputation as mean of the price paid at the specific facility 
* by type illness; if no obervation, impute the overall mean observed
*==========================================

foreach x of numlist 2 7 14 23 25 30 32 36 39 40 42 52 54 58 59 60 63 67 70 76 79 99 999   {
 set more off
 gen p_`x' = q02_40g if q02_30==`x'     /* replace the price paid at facility x with the total cost reported (this can be missing)*/ 
 forvalues illn =1/5 {
 	 display "the # subjects going to facility `x' affected by illness `illn' are:"
	 sum q02_40g if q02_30==`x' & typeill==`illn'
	 replace p_`x' = r(mean) if p_`x'==. & typeill==`illn'  /* if the total cost reported is missing, replace with the average of cost paid by type of illness*/ 
	 * count missing observations remaining  
	 display "how many missing obs remain?"
	 display ""
	 count if p_`x' ==. & typeill==`illn'   //*count how many missing obs remaining*//
	 scalar n`x'_`illn' = r(N)
 }

 display "replace with the sample average of cost paid at that facility x"
 sum q02_40g if q02_30==`x'
 display "# subjects whose cost is imputed with sample average"
 replace p_`x'=r(mean) if p_`x'==. /* if the total cost reported remains missing, replace with the sample average of cost paid at that facility x */
 *scalar list
}
					 
	
**determine the choice of hospital variable

gen choice= q02_30


gen typeprovider = .
replace typeprovider=0 if choice==99

replace typeprovider=1 if choice==2
replace typeprovider=1 if choice==30
replace typeprovider=1 if choice==39
replace typeprovider=1 if choice==23
replace typeprovider=1 if choice==67
replace typeprovider=1 if choice==32
replace typeprovider=1 if choice==69
replace typeprovider=1 if choice==51
replace typeprovider=1 if choice==59
replace typeprovider=1 if choice==7
replace typeprovider=1 if choice==68
replace typeprovider=1 if choice==40
replace typeprovider=1 if choice==70
replace typeprovider=1 if choice==66
replace typeprovider=1 if choice==76
replace typeprovider=1 if choice==54
replace typeprovider=1 if choice==25 /*** this is other category**/

replace typeprovider=2 if choice==14
replace typeprovider=2 if choice==36
replace typeprovider=2 if choice==42
replace typeprovider=2 if choice==52
replace typeprovider=2 if choice==58
replace typeprovider=2 if choice==60
replace typeprovider=2 if choice==63
replace typeprovider=2 if choice==79

replace typeprovider=4 if choice==999


* save the dataset 


xtile quartiles =aggregcons, nq(4)

save price_v4, replace

use price_v4, clear

tab quartiles typeprov if gender==2 /*FEMALE*/
tab quartiles typeprov if gender==1 /*MALE*/

clear 

******************************************************
*** TRANSFORM IN LONG FORMAT *************************
******************************************************
*use prices_v3.dta, clear
use price_v4.dta, clear

gen id = _n

*create dummy for choices
tab choice, g(d)
rename d1 d_2
rename d2 d_7
rename d3  d_14
rename d4  d_23
rename d5  d_25
rename d6  d_30
rename d7  d_32
rename d8  d_36
rename d9  d_39
rename d10  d_40
rename d11  d_42
rename d12  d_52
rename d13  d_54
rename d14  d_58
rename d15  d_59
rename d16  d_60
rename d17  d_63
rename d18  d_67
rename d19  d_70
rename d20  d_76
rename d21  d_79
rename d22  d_99
rename d23 d_999

tab typeill
tab typeill typeprovider, row
tab gender typeprovider, row
tab gender typeill, row


reshape long  d_ p_ , i(id) j(facility  2 7 14 23 25 30 32 36 39 40 42 52 54 58 59 60 63 67 70 76 79 99 999) 

label var d_ "dummy choice"
label var p_ "price as mean of observed price by illness" 

save pricelong_v4.dta, replace



